
package com.orange.browser;


import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.graphics.PixelFormat;
import android.graphics.drawable.AnimationDrawable;
import android.os.Handler;
import android.preference.PreferenceManager;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;



public class BrowserAssociatedHelp {
    static final String SHOW_FULLSCREEN_TIP = "show_fullscreen_tip";
    static final String SHOW_EFFICIENT_READ_TIP = "show_efficient_read_tip";
    static final String SHOW_SELECT_ARTICLES_TIP = "show_select_articles_tip";
    BaseUi mBaseUi;
    private UiController mUiController;
    private FullscreenTip mFullscreenTip;
    private EfficientReadTip mEfficientReadTip;
    private SelectArticlesTip mSelectArticlesTip;
    private Context mContext;
    

    public BrowserAssociatedHelp(Context context, BaseUi baseUi, UiController controller) {
        mBaseUi = baseUi;
        mContext = context;
        mUiController = controller;
        Activity act = (Activity)mContext;
        if (PreferenceManager.getDefaultSharedPreferences(context).getBoolean(
                SHOW_FULLSCREEN_TIP,
                true)) {
            mFullscreenTip = new FullscreenTip(context, mBaseUi.mContent);
        }
        if(PreferenceManager.getDefaultSharedPreferences(context).getBoolean(
                SHOW_EFFICIENT_READ_TIP,
                true)){
            mEfficientReadTip = new EfficientReadTip(context, mBaseUi.mContent);
        }
        if(PreferenceManager.getDefaultSharedPreferences(context).getBoolean(
                SHOW_SELECT_ARTICLES_TIP,
                true)){
            mSelectArticlesTip = new SelectArticlesTip(context, mBaseUi.mContent);
        }
    }

    public static UI getUI(Activity activity,Controller controller){
        UI ui;
        if (isHelpEnabled(activity)) {
            ui = new HelpEnabledPhoneUi(activity, controller);
        } else {
            ui = new PhoneUi(activity, controller);
        }
        return ui;
    }
    
    private static boolean isHelpEnabled(Activity activity){
        return PreferenceManager.getDefaultSharedPreferences(activity).getBoolean(BrowserAssociatedHelp.SHOW_FULLSCREEN_TIP,
                true) ||
                PreferenceManager.getDefaultSharedPreferences(activity).getBoolean(BrowserAssociatedHelp.SHOW_EFFICIENT_READ_TIP,
                        true);
    }
    public void onConfigurationChanged(Configuration config) {
        if (mFullscreenTip != null) {
            mFullscreenTip.update();
        }
        if(mEfficientReadTip != null){
            mEfficientReadTip.update();
        }
    }

    public boolean onBackKey() {
        if (mFullscreenTip != null && mFullscreenTip.isShown()) {
            return true;
        }
        if (mEfficientReadTip != null && mEfficientReadTip.isShown()) {
            return true;
        }
        if(null != mSelectArticlesTip && mSelectArticlesTip.isShown()){
            return true;
        }
        return false;
    }

    public void suggestHideTitleBar() {
        if (mFullscreenTip != null && mBaseUi.canHideTitleBar()
                && !mUiController.isFindDialogShowing() && !mBaseUi.isHomePageShowing()
                && !mBaseUi.getCurrentUrl().trim().equals("")) {
            mFullscreenTip.show();
        }
        
        if (mBaseUi.canHideTitleBar()) {
            if(null != mEfficientReadTip && mEfficientReadTip.isShown()){
                mEfficientReadTip.remove();
            }
        }
    }
    public void showEfficientTip(){
        showEfficientTipAccordingUrl();
    }
    
    public void showSelectArticlesTip(){
        if(null != mSelectArticlesTip){
            mSelectArticlesTip.show();
        }
    }
    public boolean isSelectArticleTipShowing(){
        if(null != mSelectArticlesTip){
            return mSelectArticlesTip.isShown();
        }
        return false;
    }
    
    public void showTitleBar() {
    }

    private void showEfficientTipAccordingUrl() {
        String currentUrl = mBaseUi.getCurrentUrl();
        boolean isWhiteUrl = Utils.isInWhiteList(mContext, currentUrl);
        if(!mBaseUi.isHomePageShowing() && isWhiteUrl){
            /**
             * this judge statment is used for the case which first close efficient tip,then
             * down fling on the fullscreen tip
             */
            if(null != mEfficientReadTip){
                mEfficientReadTip.show();
            }
           
        }
    }

    abstract class HelpTip {
        protected Context mContext;
        protected View mTip;
        protected boolean mShowTip = true;

        private SharedPreferences mSharedPref;
        private String mPreferenceKey;
        private int mResId;
        
        WindowManager mWindowManager;
        WindowManager.LayoutParams params;

        public HelpTip(Context context, int resId, ViewGroup parent, String preferenceKey) {
            mPreferenceKey = preferenceKey;
            mContext = context;
            mResId = resId;
            mSharedPref = PreferenceManager.getDefaultSharedPreferences(context);
        }

        public boolean isShown() {
            return mTip != null && mTip.isShown();
        }

        public View getTip() {
            return mTip;
        }

        public void show() {
            if (!mShowTip) {
                return;
            }
            setWindowManagerLayoutParams();
            if (mTip != null) {
                mWindowManager.removeView(mTip);
            }
            mTip = LayoutInflater.from(mContext).inflate(mResId, null);
            final View tip = mTip;
            mWindowManager.addView(mTip, params);
            View close = tip.findViewById(R.id.close);
            close.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    remove();
                }
            });
        }

        private void setWindowManagerLayoutParams() {
            mWindowManager = (WindowManager)mContext.getSystemService(Context.WINDOW_SERVICE);
            params = new WindowManager.LayoutParams();
            
            params = new WindowManager.LayoutParams();
            params.width = WindowManager.LayoutParams.MATCH_PARENT;
            params.height = WindowManager.LayoutParams.MATCH_PARENT;
            params.type = WindowManager.LayoutParams.TYPE_APPLICATION+3;
            params.format = PixelFormat.RGBA_8888;
            
            
            if(mResId == R.layout.tip_fullscreen){
                
                params.flags = WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR 
                        | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
                        | WindowManager.LayoutParams.FLAG_FULLSCREEN
                        | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                        ;  
            }
            else{
//                 if(R.layout.tip_efficient_read == mResId){
////                    params.gravity = Gravity.TOP | Gravity.LEFT;
////                    params.width = WindowManager.LayoutParams.MATCH_PARENT;
////                    int yOffset = (int)mContext.getResources().getDimension(R.dimen.efficient_read_y_offset);
////                    params.height = WindowManager.LayoutParams.MATCH_PARENT - yOffset;
////                    params.x = 0;
////                    params.y = yOffset;
//                 }
                 params.flags = WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR 
                        | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
                        | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
                        ;
            }
        }
        
        public void update() {
            if (isShown()) {
                show();
            }
        }

        public boolean remove() {
            if (mTip != null) {
                mSharedPref.edit().putBoolean(mPreferenceKey, false).commit();
                mShowTip = false;
                mWindowManager.removeView(mTip);
                mTip = null;
                resetWM();
                return true;
            }
            return false;
        }
        
        private void resetWM() {
            mWindowManager = null;
            params = null;
        }

    }
    private class FullscreenTip extends HelpTip{

        public FullscreenTip(Context context, ViewGroup parent) {
            super(context, R.layout.tip_fullscreen, parent, SHOW_FULLSCREEN_TIP);
        }

        @Override
        public void show() {
            super.show();
            if (!mShowTip) {
                return;
            }
            mTip.setOnTouchListener(new CloseFullscreenListener(mContext, mBaseUi) {
                protected void onQuitFullscreen() {
                    LogHelper.d(LogHelper.TAG_TOUCH,"BrowserAssociatedHelp.CloseFullscreenListener.onQuitFullscreen()"  );
                    remove();
                    showEfficientTipAccordingUrl();
                }
            });
            LogHelper.d(LogHelper.TAG_TOUCH,"BrowserAssociatedHelp set help CloseFullscreenListener  "  );
        }
    }
    
    private class EfficientReadTip extends HelpTip {
        float dismiss_x;
        float dismiss_y;

        public EfficientReadTip(Context context, ViewGroup parent) {
            super(context, R.layout.tip_efficient_read, parent, SHOW_EFFICIENT_READ_TIP);
            dismiss_x = context.getResources().getDimension(R.dimen.help_guide_read_dismiss_x);
            dismiss_y = context.getResources().getDimension(R.dimen.help_guide_read_dismiss_y);
        }

        public void show() {
            super.show();
            if (!mShowTip) {
                return;
            }
            mTip.setOnTouchListener(new OnTouchListener() {

                @Override
                public boolean onTouch(View v, MotionEvent event) {
                    if (MotionEvent.ACTION_UP == event.getAction()) {
                        float x = event.getX();
                        float y = event.getY();
                        if ((x > 0 && x < dismiss_x) && (y > 0 && y < dismiss_y)) {
                            remove();
                            mBaseUi.getTitleBar().getNavigationBar().startEffieientRead();
                        }
                    }
                    return false;
                }
            });
        }

    }
    
    private class SelectArticlesTip extends HelpTip {
        AnimationDrawable mHelpGuideAnimation;
        Handler mAnimationHandler;
        ImageView mReplay;
        View mSelectArticle;
        View mSelectArticleLight;

        public SelectArticlesTip(Context context, ViewGroup parent) {
            super(context, R.layout.tip_select_articles, parent, SHOW_SELECT_ARTICLES_TIP);
            mAnimationHandler = new Handler();
        }

        public void show() {
            super.show();
            if (!mShowTip) {
                return;
            }
            mSelectArticle = mTip.findViewById(R.id.select_article);
            mSelectArticleLight = mTip.findViewById(R.id.select_article_light);
            mReplay = (ImageView) mTip.findViewById(R.id.replay);
            mReplay.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    mReplay.setVisibility(View.GONE);
                    startHelpGuideAnimation();
                    mSelectArticle.setVisibility(View.VISIBLE);
                    mSelectArticleLight.setVisibility(View.GONE);
                }
            });
            final ImageView content = (ImageView) mTip.findViewById(R.id.imv_help_guide_content);
            content.setBackgroundResource(R.drawable.help_guide_content_animation);
            
            

            mHelpGuideAnimation = (AnimationDrawable) content.getBackground();
            startHelpGuideAnimationForFirstTime();
        }
        
        private void startHelpGuideAnimationForFirstTime(){
            new Thread(new Runnable() {
                
                @Override
                public void run() {
                    try {
                        Thread.sleep(50);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    LogHelper.d(LogHelper.TAG_HELP,"^^^^^^^^^^^ new Thread startHelpGuideAnimation();" );
                    mAnimationHandler.post(new Runnable() {
                        
                        @Override
                        public void run() {
                            startHelpGuideAnimation();
                        }
                    });
                    
                }
            }).start();
        }

        private void startHelpGuideAnimation() {
            if (null != mHelpGuideAnimation && mHelpGuideAnimation.isRunning()) {
                mHelpGuideAnimation.stop();
            }
            mHelpGuideAnimation.start();
            doThingsWhenAnimationEnd();
        }

        private void doThingsWhenAnimationEnd() {
            mAnimationHandler.postDelayed(new Runnable() {
                public void run() {
                    mReplay.setVisibility(View.VISIBLE);
                    mReplay.setClickable(true);
                    mSelectArticle.setVisibility(View.GONE);
                    mSelectArticleLight.setVisibility(View.VISIBLE);
                }
            }, getTotalDuration());
        }

        public int getTotalDuration() {
            int iDuration = 0;
            for (int i = 0; i < mHelpGuideAnimation.getNumberOfFrames() - 1; i++) {
                iDuration += mHelpGuideAnimation.getDuration(i);
            }
            return iDuration;
        }

    }
}
